package com.condroid;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


/**
 * This is the ShortCut Activity for the application Condroid
 * provides the UI for the short settings
 * @author Yasir Ali & Jun gao
 * Condroid Summer CSD 2011 - KTH
 */


public class ShtcutActivity extends Activity {
	private Button helpButton, saveButton;
	private EditText shtctText;
	public static String filename;
	public  SharedPreferences shtCutData;
	public static TextView textview;
	public  String loadedData;
	private String[] buffer = new String[1024];
	private int i;
	private String bufferstring;
	private int baudrate;
	private int count;
	public static String[] loadedDataa;
	private String shtcutUserText;
	private String newConfiguration;
	private String showText;
	private boolean isDeleted;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shtcut);
        setUpViews();
        
    }
	
	
	
	
	
	public String getConfiguration() {
	//	saveConfiguration(shtcutUserText);
		
		shtCutData = getSharedPreferences(filename,0);
		loadedData = shtCutData.getString("Shtcutvalues", "Not able to load");
	       // test for command retrival
     //   textview.setText(ShtcutSetting.getCommand(1));
		return loadedData;
		
	}
	
	
		
	/**
	 * This method is to set up View for the UI of ShtcutActivity
	 * It has inside save button and help button
	 * Save button calls the saveConfiguration methods which is
	 * in the Shtcutsetting class and that method saves configuration been
	 * made in the shtcut Activity and calls the method save configuration 
	 * to save.
	 * Help button has an intent to move to shtcuthelpTopic Activity
	 * 
	 */
	
	
	private void setUpViews(){
		shtctText = (EditText)findViewById(R.id.shtcutText);
		helpButton = (Button)findViewById(R.id.shtcut_help);
		saveButton = (Button)findViewById(R.id.shtcut_save);
		textview = (TextView)findViewById(R.id.textView1);
        shtCutData = getSharedPreferences(filename,0);
		loadedData = shtCutData.getString("Shtcutvalues", "Not able to load");
		shtctText.setText(loadedData);
		
	/*	showText=StdBySettings.getConfiguration(this);
		if(StdBySettings.checkAccessCode("yasir123")){
			textview.setText("Password Accepted!!");
		}else*/
	//	showText = StdBySettings.getAllowNo("12");
	//	isDeleted = StdBySettings.delAllowNo("123");
	/*	isDeleted = StdBySettings.addNumber("123");
		if(isDeleted){
		textview.setText("Number added");
		} else 		textview.setText("Already on the List");
		textview.setText("Wrong password");*/
		ShtcutSetting.getConfiguration(this);
	//	buffer = ShtcutSetting.getShortcut(1);
		
	/*	int index=ShtcutSetting.addShortcut("cd:ls:d");
		buffer=ShtcutSetting.makeCommand("ls:mkdir;ls;ls");
		if(ShtcutSetting.delShortcut(0))
	//	{
			textview.setText(Integer.toString(index));
	//	}
	//	buffer = ShtcutSetting.getShortcut(index);
		else textview.setText("Shortcut is not on the list");*/
		
		
   	
//		Saves configuraiton for shortcut setting class
		saveButton.setOnClickListener(new View.OnClickListener() {
			
		
			@Override
			public void onClick(View v) {
				shtcutUserText = shtctText.getText().toString();
				
				boolean isSaved=ShtcutSetting.saveConfiguration(shtcutUserText);  
				if(isSaved){
				makeToast("Your configuratio saved!!");
	//			Intent backToMain = new Intent(ShtcutActivity.this,MainActivity.class);
	//			startActivity(backToMain);
				}else 	makeToast("Your configuratio cannot be saved!!");

				

			}

			

		});
		
		
		helpButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ShtcutActivity.this,ShtcutHelpTopicActivity.class);
				startActivity(intent);
				
				
			}
		});
		
		
	}
 
	/***/
	
	/**
	 * Makes the toast or pops a window to display different msgs
	 * to the screen	
	 * @param text
	 */
	private void makeToast(String text) {

		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}
		
		

		
	
	
	
}

